package com.shareach.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.benayn.ustyle.string.Strs;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.redis.impl.DeployRDao;
import com.shareach.dao.redis.impl.DeployRDao.DeployType;
import com.shareach.domain.BaseBankDomain;
import com.shareach.service.BaseBankService;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;

@Controller("wbank")
@RequestMapping("/wbank")
public class BankController extends BaseController {
	
	@Autowired BaseBankService bankService;
	@Autowired DeployRDao deployRDao;
	
	@RequestMapping(value = "/dic/index.htm")
	public ModelAndView index(HttpServletRequest request, Long id) {
		ModelAndView mv = new ModelAndView("admin/dic/bank/index");
		BaseQueryOrder order = new BaseQueryOrder();
		order.setNumber(getPageNo(request).toString());
		order.setPageSize(getPageSize(request));
		order.addSort("_id", Order.DESCENDING.toString());
		
		BaseBankDomain domain = new BaseBankDomain();
		if (null != id) {
			domain.setId(id);
		}
		
		Page<BaseBankDomain> banks = bankService.getPageResult(domain, order);
		
		mv.addObject("result", banks);
		
		return mv;
	}
	
	@RequestMapping(value = "/dic/edit.htm")
	public ModelAndView bankEdit(HttpServletRequest request, HttpServletResponse response, BaseBankDomain bank) {
		ModelAndView mav = new ModelAndView("admin/dic/bank/edit");
		
		if(this.isGet(request)){
			if (null != bank && null != bank.getId()) {
				mav.addObject("item", bankService.getById(bank.getId()));
			}
		}else{
			Answer<Object> r = new Answer<Object>();
			String aliasm = this.getString(request, "aliasm", nullString);
			String telm = this.getString(request, "telm", nullString);
			if (null != aliasm) {
				bank.setAlias(Lists.newArrayList(Splitter.on(Strs.WHITE_SPACE).omitEmptyStrings().split(aliasm)).toArray(new String[]{}));
			}
			try {
				if (null != telm) {
					String[] tels = Lists.newArrayList(Splitter.on(Strs.WHITE_SPACE).omitEmptyStrings().split(telm)).toArray(new String[]{});
					for (String t : tels) {
						if (!Strs.DIGIT.matchesAllOf(t)) {
							throw new Exception("电话必须是数字."); 
						}
					}
					bank.setTel(tels);
				}
				long bankId = bankService.saveOrUpdate(bank);
				Preconditions.checkArgument(0 != bankId);
				deployRDao.addDeploy(DeployType.Bank, bankId);
			} catch (Exception e) {
				r.setCode(-1);
				r.setMsg(e.getMessage());
			}
			this.outText(response, JsonUtil.jsonFromObject(r));
		}
		
		return mav;
	}

	
}
